package com.crape.topic.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crape.account.bo.IAccountBO;
import com.crape.common.constants.ConstantCode;
import com.crape.common.enums.AttentionType;
import com.crape.common.exceptions.BOException;
import com.crape.common.model.Subject;
import com.crape.topic.bo.ISubjectBO;
import com.crape.topic.bo.ISubjectRefBO;
import com.crape.topic.dao.ISubjectDAO;
import com.crape.topic.dao.ISubjectVisitorDAO;

@Service
public class SubjectBOImpl implements ISubjectBO {

	private ISubjectDAO subjectDAOImpl;
	private ISubjectRefBO topicRefBOImpl;
	private ISubjectVisitorDAO subjectVisitorDAOImpl;
	private IAccountBO accountBOImpl;

	@Autowired
	public void setsubjectDAOImpl(ISubjectDAO subjectDAOImpl) {
		this.subjectDAOImpl = subjectDAOImpl;
	}

	@Autowired
	public void setTopicRefBOImpl(ISubjectRefBO topicRefBOImpl) {
		this.topicRefBOImpl = topicRefBOImpl;
	}

	@Autowired
	public void setsubjectVisitorDAOImpl(ISubjectVisitorDAO subjectVisitorDAOImpl) {
		this.subjectVisitorDAOImpl = subjectVisitorDAOImpl;
	}

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@Override
	public List<Subject> getAllSubjects() throws BOException {
		return subjectDAOImpl.getSubjects(null);
	}

	@Override
	public List<Subject> getAllSubjects(Integer startIndex, Integer pageSize) throws BOException {
		if (startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		List<Subject> resultList = subjectDAOImpl.getAllSubjects(startIndex, pageSize);
		return resultList;
	}

	@Override
	public List<Subject> getAllSubjects(Long accountid, Integer startIndex, Integer pageSize) throws BOException {
		if (startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		List<Subject> resultList = subjectDAOImpl.getAllSubjects(startIndex, pageSize);
		if (accountid != null && resultList != null && !resultList.isEmpty()) {
			for (Subject obj : resultList) {
				Long subjectid = obj.getId();
				Integer isAtt = this.isSubjectAttention(accountid, subjectid);
				obj.setIsattention(isAtt);
			}
		}
		return resultList;
	}

	@Override
	public Integer isSubjectAttention(Long accountid, Long subjectid) {
		// 是否关注
		Integer mySubNum = topicRefBOImpl.countSubjectAttention(accountid, subjectid);
		if (mySubNum != null && mySubNum > 0) {
			return AttentionType.yes.getCode();
		} else {
			return AttentionType.no.getCode();
		}
	}

	@Override
	public Long countAllSubjects() throws BOException {
		return subjectDAOImpl.countAllSubjects();
	}

	@Override
	public List<Subject> listSubjects(Subject subject) throws BOException {
		if (subject == null) {
			return null;
		}
		return subjectDAOImpl.listSubjects(subject);
	}

	@Override
	public List<Subject> getHotSubjects() throws BOException {
		return subjectDAOImpl.getHotSubjects();
	}

	@Override
	public Subject getSubjectById(Long id) throws BOException {
		if (id == null) {
			return null;
		}
		Subject params = new Subject();
		params.setId(id);
		List<Subject> list = subjectDAOImpl.getSubjects(params);
		if (list == null || list.isEmpty()) {
			return null;
		}
		Subject subject = list.get(0);
		return subject;
	}

	@Override
	public void addSubject(Subject subject) throws BOException {
		if (subject == null) {
			return;
		}
		if(subject.getImagename()==null){
			subject.setImagename(ConstantCode.default_image);
		}
		subjectDAOImpl.insertSubject(subject);
	}

	@Override
	public void updateSubject(Subject subject) throws BOException {
		if (subject == null) {
			return;
		}
		subjectDAOImpl.updateSubject(subject);
	}

	@Override
	public void deleteSubjectById(Long id) throws BOException {
		if (id == null) {
			return;
		}
		subjectDAOImpl.deleteSubjectById(id);
	}

	@Override
	public Long countAllAttentions(Long accountid) throws BOException {
		if (accountid == null) {
			return 0L;
		}
		return subjectDAOImpl.countSubjectAttentionBySubjectid(accountid);
	}

	@Override
	public List<Subject> getTop8Attentions(Long accountid) throws BOException {
		if (accountid == null) {
			return null;
		}
		return subjectDAOImpl.getTop8SubjectAttentions(accountid);
	}

	@Override
	public List<Subject> getSubjectAttentionsByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws BOException {
		if (accountid == null || startIndex == null || pageSize == null) {
			return null;
		}
		return subjectDAOImpl.getSubjectAttentionsByAccountid(accountid, startIndex, pageSize);
	}

	@Override
	public Long countSubjectAttentionsByAccountid(Long accountid) throws BOException {
		if (accountid == null) {
			return null;
		}
		return subjectDAOImpl.countSubjectAttentionsByAccountid(accountid);
	}

	@Override
	public void addSubjectVisitor(Long visitorid, Long subjectid) throws BOException {
		if (visitorid == null || subjectid == null) {
			return;
		}
		Long count = subjectVisitorDAOImpl.countSubjectVisitor(visitorid, subjectid);
		if (count != null && count > 0) {
			subjectVisitorDAOImpl.updateSubjectVisitor(visitorid, subjectid);
		} else {
			subjectVisitorDAOImpl.insertSubjectVisitor(visitorid, subjectid);
		}
	}
}
